package com.example.websocket.task;

import com.example.websocket.server.MyWebSocket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @author: dtYuMa
 * @date: 2021年08月19日 9:24
 * @description:
 */

@Component
@EnableScheduling
public class TimeTask {
    private static Logger logger = LoggerFactory.getLogger(TimeTask.class);

    //@Scheduled(cron = "0/5 * * * * ?")   //每分钟执行一次
    //@Scheduled(fixedRate = 5000)
    public void test() {
        System.err.println("*********   定时任务执行   **************");
        CopyOnWriteArraySet<MyWebSocket> webSocketSet = MyWebSocket.getWebSocketSet();
        int i = 0;
        webSocketSet.forEach(c -> {
            try {
                c.sendMessage("  定时发送  " + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        System.err.println("定时任务完成.......");
    }
}
